/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.SysLogsPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  SysLogsPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class SysLogsSqlProvider {
	public static final String TABLE_NAME = "sys_logs";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param sysLogs
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSysLogss(final SysLogsPo sysLogs) {
		return new CustomerSQL() {{
			SELECT("create_time,description,id,module_id,module_name,op_type,operator_id,operator_name ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(sysLogs.getDescription())) {
				WHERE("description=#{description}");
			}
			if (null != sysLogs.getModuleId()) {
				WHERE("module_id=#{moduleId}");
			}
			if (StringUtils.isNotBlank(sysLogs.getModuleName())) {
				WHERE("module_name=#{moduleName}");
			}
			if (null != sysLogs.getOpType()) {
				WHERE("op_type=#{opType}");
			}
			if (null != sysLogs.getOperatorId()) {
				WHERE("operator_id=#{operatorId}");
			}
			if (StringUtils.isNotBlank(sysLogs.getOperatorName())) {
				WHERE("operator_name=#{operatorName}");
			}
			if(StringUtils.isNotBlank(sysLogs.getSortField())) {
				ORDER_BY(sysLogs.getSortField() + (StringUtils.isNotBlank(sysLogs.getSortMethod()) ? "desc" : sysLogs.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != sysLogs.getStart() && null != sysLogs.getSize()) {
				LIMIT(sysLogs.getStart(), sysLogs.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param sysLogs
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSysLogsCount(final SysLogsPo sysLogs) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(sysLogs.getDescription())) {
				WHERE("description=#{description}");
			}
			if (null != sysLogs.getModuleId()) {
				WHERE("module_id=#{moduleId}");
			}
			if (StringUtils.isNotBlank(sysLogs.getModuleName())) {
				WHERE("module_name=#{moduleName}");
			}
			if (null != sysLogs.getOpType()) {
				WHERE("op_type=#{opType}");
			}
			if (null != sysLogs.getOperatorId()) {
				WHERE("operator_id=#{operatorId}");
			}
			if (StringUtils.isNotBlank(sysLogs.getOperatorName())) {
				WHERE("operator_name=#{operatorName}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSysLogsById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param sysLogs
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addSysLogs(final SysLogsPo sysLogs) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(sysLogs.getDescription())) {
				VALUES("description","#{description}");
			}
			if (null != sysLogs.getModuleId()) {
				VALUES("module_id","#{moduleId}");
			}
			if (StringUtils.isNotBlank(sysLogs.getModuleName())) {
				VALUES("module_name","#{moduleName}");
			}
			if (null != sysLogs.getOpType()) {
				VALUES("op_type","#{opType}");
			}
			if (null != sysLogs.getOperatorId()) {
				VALUES("operator_id","#{operatorId}");
			}
			if (StringUtils.isNotBlank(sysLogs.getOperatorName())) {
				VALUES("operator_name","#{operatorName}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param sysLogs
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateSysLogs(final SysLogsPo sysLogs) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(sysLogs.getDescription())) {
				SET("description=#{description}");
			}
			if (null != sysLogs.getModuleId()) {
				SET("module_id=#{moduleId}");
			}
			if (StringUtils.isNotBlank(sysLogs.getModuleName())) {
				SET("module_name=#{moduleName}");
			}
			if (null != sysLogs.getOpType()) {
				SET("op_type=#{opType}");
			}
			if (null != sysLogs.getOperatorId()) {
				SET("operator_id=#{operatorId}");
			}
			if (StringUtils.isNotBlank(sysLogs.getOperatorName())) {
				SET("operator_name=#{operatorName}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delSysLogs(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
